﻿Imports System
Imports System.Collections
Imports System.Configuration
Imports System.Collections.Specialized

Namespace Framework
    Public Class SystemConfiguration
        Implements IConfigurationSectionHandler
        '
        ' Constant values for all expected entries in the ictusConfiguration section
        '
        Private Const DATAACCESS_UID As String = "config.DataAccess.UID"
        Private Const DATAACCESS_PWD As String = "config.DataAccess.PWD"
        Private Const DATAACCESS_SERVER As String = "config.DataAccess.SERVER"
        Private Const DATAACCESS_DATABASE As String = "config.DataAccess.DATABASE"
        Private Const DATAACCESS_CONNECTIONSTRING As String = "config.DataAccess.ConnectionString"

        Private Const DATAACCESS_CURRENTINFO As String = "config.DataAccess.CultureInfo"
        Private Const DATAACCESS_CULTURETHAI As String = "config.DataAccess.CultureThai"
        '
        ' Static member variables. These contain the application settings
        '   from Config.Web, or the default values.
        '
        Private Shared fieldUID As String
        Private Shared fieldPWD As String
        Private Shared fieldSERVER As String
        Private Shared fieldDATABASE As String
        Private Shared fieldConnectionString As String

        Private Shared fieldCurrentInfo As String
        Private Shared fieldCultureThai As String
        '
        ' Constant values for all of the default settings.
        '
        Private Const DATAACCESS_UID_DEFAULT As String = "SA"
        Private Const DATAACCESS_PWD_DEFAULT As String = ""
        Private Const DATAACCESS_SERVER_DEFAULT As String = "LOCALHOST"
        Private Const DATAACCESS_DATABASE_DEFAULT As String = "D7"
        Private Const DATAACCESS_CONNECTIONSTRING_DEFAULT As String = "SERVER= LOCALHOST; UID= SA; DATABASE= D7"

        Private Const DATAACCESS_CURRENTINFO_DEFAULT As String = "en"
        Private Const DATAACCESS_CULTURETHAI_DEFAULT As String = "th-TH"

        Public Function Create(ByVal parent As Object, ByVal configContext As Object, ByVal input As Xml.XmlNode) As Object Implements IConfigurationSectionHandler.Create
            Dim settings As NameValueCollection

            Try
                Dim baseHandler As NameValueSectionHandler
                baseHandler = New NameValueSectionHandler
                settings = CType(baseHandler.Create(parent, configContext, input), NameValueCollection)
            Catch
            End Try

            If settings Is Nothing Then
                fieldUID = DATAACCESS_UID_DEFAULT
                fieldPWD = DATAACCESS_PWD_DEFAULT
                fieldSERVER = DATAACCESS_SERVER_DEFAULT
                fieldDATABASE = DATAACCESS_DATABASE_DEFAULT
                fieldConnectionString = DATAACCESS_CONNECTIONSTRING_DEFAULT

                fieldCurrentInfo = DATAACCESS_CURRENTINFO_DEFAULT
                fieldCultureThai = DATAACCESS_CULTURETHAI_DEFAULT
            Else
                fieldUID = ApplicationConfig.ReadSetting(settings, DATAACCESS_UID, DATAACCESS_UID_DEFAULT)
                fieldPWD = ApplicationConfig.ReadSetting(settings, DATAACCESS_PWD, DATAACCESS_PWD_DEFAULT)
                fieldSERVER = ApplicationConfig.ReadSetting(settings, DATAACCESS_SERVER, DATAACCESS_SERVER_DEFAULT)
                fieldDATABASE = ApplicationConfig.ReadSetting(settings, DATAACCESS_DATABASE, DATAACCESS_DATABASE_DEFAULT)
                fieldConnectionString = ApplicationConfig.ReadSetting(settings, DATAACCESS_CONNECTIONSTRING, DATAACCESS_CONNECTIONSTRING_DEFAULT)

                fieldCurrentInfo = ApplicationConfig.ReadSetting(settings, DATAACCESS_CURRENTINFO, DATAACCESS_CURRENTINFO_DEFAULT)
                fieldCultureThai = ApplicationConfig.ReadSetting(settings, DATAACCESS_CULTURETHAI, DATAACCESS_CULTURETHAI_DEFAULT)
            End If
        End Function

        '----------------------------------------------------------------
        ' Property Get User ID:
        '   Retrieves the ictus's database connection string.
        ' Returns:
        '   The database connection string 
        '----------------------------------------------------------------
        Public Shared ReadOnly Property UID() As String
            Get
                UID = fieldUID
            End Get
        End Property

        '----------------------------------------------------------------
        ' Property Get Password:
        '   Retrieves the ictus's database connection string.
        ' Returns:
        '   The database connection string 
        '----------------------------------------------------------------
        Public Shared ReadOnly Property PWD() As String
            Get
                PWD = fieldPWD
            End Get
        End Property

        '----------------------------------------------------------------
        ' Property Get Server:
        '   Retrieves the ictus's database connection string.
        ' Returns:
        '   The database connection string 
        '----------------------------------------------------------------
        Public Shared ReadOnly Property SERVER() As String
            Get
                SERVER = fieldSERVER
            End Get
        End Property

        '----------------------------------------------------------------
        ' Property Get DataBase:
        '   Retrieves the ictus's database connection string.
        ' Returns:
        '   The database connection string 
        '----------------------------------------------------------------
        Public Shared ReadOnly Property DATABASE() As String
            Get
                DATABASE = fieldDATABASE
            End Get
        End Property

        '----------------------------------------------------------------
        ' Property Get ConnectionString:
        '   Retrieves the ictus's database connection string.
        ' Returns:
        '   The database connection string 
        '----------------------------------------------------------------
        Public Shared ReadOnly Property ConnectionString() As String
            Get
                ConnectionString = fieldConnectionString
            End Get
        End Property

        '----------------------------------------------------------------
        ' Property Get CultureInfo:
        '   Retrieves the ictus's database connection string.
        ' Returns:
        '   The database connection string 
        '----------------------------------------------------------------
        Public Shared Property CultureInfo() As String
            Get
                CultureInfo = fieldCurrentInfo
            End Get
            Set(ByVal Value As String)
                fieldCurrentInfo = Value
            End Set
        End Property

        '----------------------------------------------------------------
        ' Property Get CultureThai:
        '   Retrieves the ictus's database connection string.
        ' Returns:
        '   The database connection string 
        '----------------------------------------------------------------
        Public Shared ReadOnly Property CultureThai() As String
            Get
                CultureThai = fieldCultureThai
            End Get
        End Property
    End Class
End Namespace